package com.lgr.store.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgr.store.service.CharactersService;
import com.lgr.store.service.DepService;
import com.lgr.store.service.EmpService;
import com.lgr.store.utils.AesUtils;
import com.lgr.store.utils.AppointStateEnum;
import com.lgr.store.utils.LogJournal;
import com.lgr.store.vo.DepVo;
import com.lgr.store.vo.EmpVo;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/admin")
public class LoginController {
    @Resource
    EmpService empService;
    @Resource
    HttpSession session;
    @Resource
    private CharactersService charactersService;
    @Resource
    private DepService depService;

    /*
     * 登录界面读取cookie
     * @param cardNo 身份证号或者手机号
     * @param password 密码
     * @return cardNo 身份证号或者手机号
     * @return password 密码
     * @return login jsp页面
     * */
    @RequestMapping(value = "/logins", method = RequestMethod.GET)
    public String index(@CookieValue(value = "cardNo", required = false, defaultValue = "") String cardNo,
                        @CookieValue(value = "password", required = false, defaultValue = "") String password, ModelMap map) {
        map.addAttribute("cardNo", cardNo);
        map.addAttribute("password", password);
        return "login";
    }

    /*
     *登录
     * @param cardNo 身份证号或者手机号
     * @param password 密码
     * @param rememberPwd 是否记住密码
     * @param smscode 验证码
     * @return empName 员工姓名
     * @return index 主界面jsp
     * @return login 登录界面jsp
     * @return msg 提示信息
     * */
    @RequestMapping(value = "/logins", method = RequestMethod.POST)
    public String login(HttpServletRequest request, HttpServletResponse response, Model model, ModelMap map) {
        String cardNo = request.getParameter("cardNo");//手机号码
        String password = request.getParameter("password");//登录密码
        String rememberPwd = request.getParameter("rememberPwd");//是否记住密码
        String smscode = request.getParameter("smscode");//验证码
        String smscode1 = (String) request.getSession().getAttribute("codes"); //系统生成的验证码
        //验证码统一转换为大写字母，
        smscode = smscode.toUpperCase();
        smscode1 = smscode1.toUpperCase();
        if(!smscode.equals(smscode1)){
            model.addAttribute("msg", "验证码错误");
            model.addAttribute("cardNo",cardNo);
            model.addAttribute("password",password);
            return "login";
        }
        EmpVo emp = new EmpVo();
        emp.setPhone(cardNo);
        //对密码进行加密
//        String passwords = AesUtils.encryptStr(password, AesUtils.SECRETKEY);
        String passwords = password;

        //登录查询
        emp = empService.login(emp);
        if(emp==null){
            model.addAttribute("msg", "用户名错误");
            model.addAttribute("cardNo",cardNo);
            model.addAttribute("password",password);
            return "login";
        }
        if(!passwords.equals(emp.getPassword())){
            model.addAttribute("msg", "密码错误");
            model.addAttribute("cardNo",cardNo);
            model.addAttribute("password",password);
            return "login";
        }
        //判断用户状态是否已禁用
        if(emp.getStatus()==0){
            model.addAttribute("msg", "用户已被禁用");
            model.addAttribute("cardNo", cardNo);
            model.addAttribute("password", password);
            return "login";
        }
        //获取用户权限
        emp.setModuleEmpList(charactersService.findEmpChar(emp.getEmpId()));
        //设置session
        request.getSession().setAttribute(AppointStateEnum.EMPS.getStateInfo(), emp);
        //把用户名称与密码保存到cookie中
        if (rememberPwd != null) {
            Cookie ccardNo = new Cookie("cardNo", URLEncoder.encode(cardNo));
            ccardNo.setMaxAge(60 * 60 * 24);
            Cookie cpassword = new Cookie("password", URLEncoder.encode(password));
            cpassword.setMaxAge(60 * 60 * 24);
            ccardNo.setPath(request.getContextPath());
            cpassword.setPath(request.getContextPath());
            response.addCookie(ccardNo);
            response.addCookie(cpassword);
        }
        //写日志操作
        session.setAttribute(AppointStateEnum.LOG.getStateInfo(), LogJournal.setLogParam(4, emp));
        return "index";
    }

    @RequestMapping(value = "/error")
    private String error() {
        return "error";
    }

    @RequestMapping(value = "/exit")
    private String exit() {
        return "exit";
    }

    @RequestMapping(value = "/index")
    private String index() {
        return "index";
    }

    @RequestMapping(value = "/login")
    private String login() {
        return "login";
    }

    @RequestMapping(value = "/enimg")
    private String enimg() {
        return "enimg";
    }
}
